
package com.joysuch.wwyt.dataperm.context;

import java.util.Stack;

public class DataPermContextHolder {

    /**
     * TenantAndOrgCode数据拦截，是否对join进行特殊处理
     */
    static ThreadLocal<Boolean> tenantAndOrgCodeJoinSwitch = new ThreadLocal<>();

    static ThreadLocal<Boolean> devPrintLoggerHolder = new ThreadLocal<>();

    static ThreadLocal<DataPermContext> contextHolder = new ThreadLocal<>();
    // 接口级数据权限是否启用开关
    static ThreadLocal<Boolean> topSwitchHolder = new ThreadLocal<>();
    static ThreadLocal<Stack<DataPermEnableState>> datapermSwitchHolder = new ThreadLocal<>();

    public static DataPermContext getContext() {
        return contextHolder.get();
    }

    public static void setContext(DataPermContext context) {
        contextHolder.set(context);
    }

    public static void clearContext() {
        contextHolder.remove();
    }

    public static void setTenantAndOrgCodeJoinSwitchFlag(boolean s) {
        tenantAndOrgCodeJoinSwitch.set(s);
    }

    public static void clearTenantAndOrgCodeJoinSwitchFlag() {
        tenantAndOrgCodeJoinSwitch.remove();
    }

    public static boolean isTenantAndOrgCodeJoinSwitchFlag() {
        Boolean b = tenantAndOrgCodeJoinSwitch.get();
        return b != null && b.booleanValue() == true;
    }

    public static void setPrintDevLog(boolean s) {
        devPrintLoggerHolder.set(s);
    }

    public static void clearDevPrintLog() {
        devPrintLoggerHolder.remove();
    }

    public static boolean isPrintDevLog() {
        Boolean b = devPrintLoggerHolder.get();
        return b == null ? false : b;
    }

    public static void setDataPermTopSwitch(boolean s) {
        topSwitchHolder.set(s);
    }

    public static void clearDataPermTopSwitch() {
        topSwitchHolder.remove();
    }

    public static boolean getDataPermTopSwitch() {
        Boolean b = topSwitchHolder.get();
        return b == null ? false : b;
    }

    public static void pushCurrentDataPermSwitch(boolean enabled) {
        pushCurrentDataPermSwitch(enabled, false);
    }

    public static void pushCurrentDataPermSwitch(boolean enabled, boolean recursive) {
        Stack<DataPermEnableState> stack = datapermSwitchHolder.get();
        if (stack == null) {
            stack = new Stack<DataPermEnableState>();
            datapermSwitchHolder.set(stack);
        }
        if (stack.isEmpty()) {
            stack.push(new DataPermEnableState(enabled, recursive));
        } else {
            DataPermEnableState peek = stack.peek();
            if (peek.recursive) {
                // 如果当前是递归调用，则前置后面的状态和前面保持一致
                stack.push(peek);
            } else {
                stack.push(new DataPermEnableState(enabled, recursive));
            }
        }

    }

    public static boolean popDataPermSwitch() {
        Stack<DataPermEnableState> stack = datapermSwitchHolder.get();
        if (stack == null || stack.isEmpty()) {
            throw new IllegalStateException("当前栈中没有数据权限开关值");
        }
        return stack.pop().enabled;
    }

    public static boolean isDataPermEnabled() {
        if (!getDataPermTopSwitch()) {
            // 没有开启顶部开关
            return false;
        }
        Stack<DataPermEnableState> stack = datapermSwitchHolder.get();
        if (stack == null || stack.isEmpty()) {
            // 栈为空，此时接口已经默认打开数据拦截
            return true;
        }
        return stack.peek().enabled;
    }
}
